package java8.timeutil;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author Cheng Jun
 * @version 1.0
 * Description: java.time.Clock类使用时区提供对当前即时，日期和时间的访问。
 * @date 2019/8/11 15:46
 */
public class ClockStudy {

    public static void main(String[] args) {
        // SystemClock[Z] Z：是偏移量为0的id，也就是格里尼治当地日期时间
        Clock clock = Clock.systemUTC();
        long millis = clock.millis();

        // 2019-08-16T01:32:19.918Z
        Instant instant = clock.instant();
        System.out.println(instant);

        // Z
        ZoneId zoneId = clock.getZone();
        // ZoneId.getAvailableZoneIds().forEach(id-> System.out.println(id));

        // SystemClock[+08:00]
        // ZoneId.of("+19:00") throws java.time:DateTimeException: Zone offset hours not in valid range: value 19 is not in the range -18 to 18
        Clock beijingClock = clock.withZone(ZoneId.of("+08:00"));

        // SystemClock[Asia/Shanghai]
        Clock defaultClock = Clock.systemDefaultZone();

        // 2019-08-16T02:15:48.076Z
        Instant defaultInstant = defaultClock.instant();

        // 2019-08-16T10:42:11.701
        LocalDateTime localDateTime = LocalDateTime.now(defaultClock);

        // 2019-08-16T02:42:11.701
        LocalDateTime greenwichDateTime = LocalDateTime.now(clock);

        // 2019-08-16T02:42:11.701
        LocalDateTime greenwichDateTime1 = LocalDateTime.now(zoneId);

        System.out.println(beijingClock);
        System.out.println(defaultClock);
        System.out.println(localDateTime);
        System.out.println(greenwichDateTime);
        System.out.println(greenwichDateTime1);
    }
}
